from django.db import models
from thinghoo_library.models import TimeModel
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from django.urls import reverse

# Create your models here.


def quick_charfield():
    return models.CharField(max_length=255)


def select_storage():
    if settings.DEBUG:
        task_fs = FileSystemStorage('./data/task')
    else:
        task_fs = FileSystemStorage('/data/task')
    return task_fs


class Server(TimeModel):
    name = models.CharField(max_length=255)
    host = quick_charfield()
    username = quick_charfield()
    port = models.IntegerField(default=22)
    keyfile = models.FileField(upload_to='key', storage=select_storage, null=True, blank=True)

    remote_path = models.CharField('服务器构建路径', max_length=255)

    class Meta:
        permissions = (
            ('server_add', 'can add server'),
            ('server_list', 'can view server'),
            ('server_edit', 'can edit server'),
        )

    def __str__(self):
        return f'<Server {self.name}>'

    def get_absolute_url(self):
        return reverse("server_detail", kwargs={"pk": self.pk})

